package com.DreamForce.demo.integrate;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.DreamForce.Demo.persist.IbatisImpl.AccountHandler;
import com.DreamForce.demo.model.Account;
@Service
public class AccountService {
	
	protected static Logger logger = Logger.getLogger(AccountHandler.class);
	
	@Autowired
	private AccountHandler accountHandler;
	
	public Account getAccount(String username)
	{
		return accountHandler.getAccount(username);
	}
	 public Account getAccount(String username, String password) 
	 {
		 return accountHandler.getAccount(username, password);
	 }
	 public boolean createAccount(Account account)
	 {
		 accountHandler.insertAccount(account);
		 accountHandler.insertProfile(account);
		 accountHandler.insertSignon(account);
		 return true;
	 }
	 public boolean updateAccount(Account account)
	 {
		 int version = accountHandler.getAccountVersion(account.getUsername());
			boolean flag = false;
			if(version == account.getVersion())
			{
				accountHandler.updateAccount(account);
				accountHandler.updateProfile(account);
			    if (account.getPassword() != null && account.getPassword().length() > 0) 
			      {
			    	accountHandler.updateSignon(account);
			      }
			    flag = true;
			}
		    return flag;
	 }
	 

}
